package org.jeecg.modules.demo3.fly.entity.tcp;

import redis.clients.jedis.Client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class SocketClients implements Runnable {
    // private static ConcurrentHashMap<String, Socket> map = new ConcurrentHashMap();
    // private String number = null;
    private Socket socket;
    private Clents clents;
    private DataInputStream input;
    private DataOutputStream output;
    List<Clents> clentsList;

    public SocketClients(Clents s) {
        this.clents = s;
        try {
            this.input = new DataInputStream(clents.getSocket().getInputStream());
            this.output = new DataOutputStream(clents.getSocket().getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


//    public SocketClients(Socket s) {
//        this.socket = s;
//        try {
//            this.input=new DataInputStream(socket.getInputStream());
//            this.output=new DataOutputStream(socket.getOutputStream());
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }

    @Override
    public void run() {
        while (true) {
            byte[] bufferByte = new byte[1024];
            try {
                int readlength = input.read(bufferByte, 0, bufferByte.length);
                System.out.println("读取到的数据===" + Arrays.toString(bufferByte));
                List<Clents> clentsList = TCPServer.clentsList;
                System.out.println(clentsList.size());
                for (Clents clentsitem : clentsList) {
                    if (clentsitem.getNumber() == clents.getNumber() && clentsitem.getSocket() != clents.getSocket()) {
                        System.out.println("clentsitem.getNumber()==========="+clentsitem.getNumber()+"--------clents.getNumber()=============== "+clents.getNumber());
                        output =  new DataOutputStream(clentsitem.getSocket().getOutputStream());
                        output.write(bufferByte, 0, readlength);
                        output.flush();
                    }
                }







                //  ConcurrentHashMap<String, Socket> concurrentHashMap = TCPServer.concurrentHashMap;
//                ConcurrentHashMap.KeySetView<String, Socket> keySet = concurrentHashMap.keySet();
//                for (String serial_number : keySet) {
//                    Socket socket = concurrentHashMap.get(serial_number);
//                    if (this.socket == socket){//转发到指定飞机设备
//                        if (!this.socket.isClosed() && !socket.isClosed()){
//                            output.write(bufferByte);
//                            output.flush();
//                        }
//                    }
//                }


//                 clentsList = TCPServer.clentsList;
//                for (Clents clents1 : clentsList) {
//                    if (!clents1.getSocket().isClosed() && !clents.getSocket().isClosed() && clents != clents1 && clents.getNumber() == clents1.getNumber()) {
//                        output = new DataOutputStream(clents1.getSocket().getOutputStream());
//                        output.write(bufferByte);
//                        output.flush();
//                    }
//                }


//                ConcurrentHashMap<String, Clents> concurrentHashMap = TCPServer.concurrentHashMap;
//                ConcurrentHashMap.KeySetView<String, Clents> keySet = concurrentHashMap.keySet();
//                for (String serial_number : keySet) {
//                    Clents client = concurrentHashMap.get(serial_number);
//                }


            } catch (IOException e) {
                e.printStackTrace();
            }


//            finally {
//                try {
//                    input.close();
//                    output.close();
//                    clents.getSocket().close();
//                    clentsList.remove(clents);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
        }
    }


}
